/**
 * Created by renzhiqiang on 17/5/6.
 */
import {Breadcrumb, Icon, Table, Pagination} from 'antd';
import React, {PropTypes} from 'react'
import style from './index.less'

const tableView = (props) => {
  const {rowSelection, pageParam, onChange, dataSource, columns, loading, contentMenuProps, styleShow, isCorTable, rowKey, linkHead} = props;
  const newCol = columns.map(function (item, key) {
    if (item.optType ) {
      if(item.optType == 'PopMenu' && contentMenuProps){
        item.render = (text, record, index) => {
          let tmp = () =>
            <ContentMenu {...contentMenuProps}
                         index={index} record={record}/>
          return (
            <Popover placement="bottom" content={tmp()} trigger="click">
              <a>{text}</a>
            </Popover>
          );
        };
    } else if(item.optType == 'Link' && linkHead){
        item.render = (text, record, index) => <a href={`${linkHead}${record[rowKey]}`}>{text}</a>;
      }
      return item;
    } else if (item.isCode && item.isCode == 'qrcode') {
      item.render = (text, record) => {
        let tmp = () =>
          <img src={record.qrcodeUrl} style={{width: '200px', height: '200px'}}/>
        return (
          <Popover placement="bottom" content={tmp()}>
            {text}
          </Popover>
        );
      };
      return item;
    } else {
      return item;
    }
  });
  const pagination = {
    total: dataSource.total,
    pageSize: pageParam.pageSize,
    pageSizeOptions: ['1','10', '20', '30', '40'],
    showTotal: total => `共 ${total} 条`,
    showSizeChanger: true,
    showQuickJumper: true,
    onShowSizeChange: pageParam.onShowSizeChange,
    onChange: pageParam.onChange,
    size: "small"
  };
  if (pageParam.current) {
    pagination.current = pageParam.current;
  }
  //pagination={pagination}  style={{marginTop: styleShow ? '40px' : ''}}
  return <div style={{marginTop: styleShow ? '40px' : ''}}>
    <Table rowSelection={rowSelection}
           bordered
           rowKey={record => record[rowKey] }
           columns={newCol}
           loading={loading}
           dataSource={dataSource.datas}
           onChange={onChange || ''}
           pagination={isCorTable ? false : pagination}
           size="middle"
    />
    {
      isCorTable ? (
        (dataSource.datas && dataSource.datas.length > 0) ?
          <div className={style.paginationBox}>
            <Pagination {...pagination}/>
          </div>
          : ''
      ) : ''
    }
  </div>
};
export default tableView
